package com.zcoj.controller.User.group;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.Group;
import com.zcoj.pojo.Dto.GroupAnnouncement;
import com.zcoj.pojo.Vo.GroupMainReqVO;
import com.zcoj.pojo.Vo.GroupMemberRegisterVO;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.GroupAnnouncementService;
import com.zcoj.service.GroupService;
import com.zcoj.util.RedisCaches;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 接口
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@RestController
@RequestMapping("/group")
@Api(tags = "接口")
public class GroupController  {

    @Autowired
    private GroupService groupService;

    @Autowired
    private RedisCaches redisCaches;


    @Autowired
    private GroupAnnouncementService groupAnnouncementService;
    /**
     * 创建
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/create")
    @ApiOperation("新建")
    public ApiResponse create(@RequestBody Group group) {
        Integer integer = groupService.create(group);
        if(Objects.nonNull(integer)){
            return ApiResponse.success("新建团队成功，等待管理员审核！！！",integer);
        }
        return ApiResponse.failed("新建团队失败");
    }

    /**
     * 删除
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    public ApiResponse deleteById(@PathVariable Integer id) {
        groupService.deleteById(id);
        return ApiResponse.success(null);
    }

    /**
     * 根据ID修改
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/updateById")
    @ApiOperation("根据ID修改")
    public ApiResponse updateById(@RequestBody Group group) {
        groupService.updateById(group);
        return ApiResponse.success(null);
    }

    /**
     * 分页查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public ApiResponse findPage (@RequestBody PageWrap<Group> pageWrap) {
        return ApiResponse.success(groupService.findPage(pageWrap));
    }

    @ApiModelProperty("分页查询")
    @PostMapping("/pageVo")
    public ApiResponse findPageVo(@RequestBody PageWrap<Group> pageWrap){
        LoginUser loginUser = redisCaches.getLoginUser();
        if(!(loginUser.getPermission().contains("admin")||loginUser.getPermission().contains("super_admin")||loginUser.getPermission().contains("group_admin"))){
            pageWrap.getModel().setStatus(1);
        }else {
            pageWrap.getModel().setStatus(null);
        }
        return ApiResponse.success(groupService.findPageVo(pageWrap));
    }

    @PostMapping("/groupMemberRegisterVO")
    public ApiResponse memberRegister(@RequestBody GroupMemberRegisterVO groupMemberRegisterVO){
        Boolean memberRegister = groupService.memberRegister(groupMemberRegisterVO);
        if(memberRegister){
            return ApiResponse.success("请求申请成功",null);
        }else {
            return ApiResponse.failed("请求申请失败");
        }
    }

    @PostMapping("/groupMainData")
    public ApiResponse groupMainData(@RequestBody GroupMainReqVO groupMainReqVO){
       return groupService.groupMainData(groupMainReqVO);
    }


    @GetMapping("/quitGroup/{id}")
    public ApiResponse quitGroup(@PathVariable Integer id){
        Boolean quitGroup = groupService.quitGroup(id);
        if(quitGroup){
            return ApiResponse.success("退出团队成功",null);
        }else {
            return ApiResponse.failed("退出团队失败");
        }
    }



    /**
     * 通过ID查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public ApiResponse finById(@PathVariable Integer id) {
        return ApiResponse.success(groupService.findById(id));
    }




    @GetMapping("/getGroupAnnouncementId/{gid}")
    public ApiResponse getGroupAnnouncementId(@PathVariable Integer gid){
        GroupAnnouncement groupAnnouncement = new GroupAnnouncement();
        groupAnnouncement.setGid(gid);
        GroupAnnouncement groupAnnouncementServiceOne = groupAnnouncementService.findOne(groupAnnouncement);
        if( Objects.nonNull(groupAnnouncementServiceOne)){
            return ApiResponse.success(groupAnnouncementServiceOne.getAid());
        }else {
            return ApiResponse.success(0);
        }
    }
}
